package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.xgkj.diyiketang.EatShowApp;
import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.DatiDetailActivity;
import com.xgkj.diyiketang.data.DatiInfo;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ToastUtil;
import com.xgkj.diyiketang.util.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/7/7.
 */
public class DatiAdapter extends BaseAdapter {
    private Context mContext;
    private List<DatiInfo> mList = new ArrayList<DatiInfo>();
    private int mUserType;

    public DatiAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<DatiInfo> list, int userType) {
        mList.addAll(list);
        notifyDataSetChanged();
        mUserType = userType;
    }

    public void clearData() {
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList.size() > 0) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.dati_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        if (position >= mList.size()) {
            return convertView;
        }
        initializeViews(mList.get(position), (ViewHolder) convertView.getTag());
        final DatiInfo info = mList.get(position);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (info != null && mUserType == 0) {
                    Intent detailIntent = new Intent(mContext, DatiDetailActivity.class);
                    detailIntent.putExtra("DATIINFO", info);
                    mContext.startActivity(detailIntent);
                } else {
                    ToastUtil.showMessage(mContext, "仅学生可答题");
                }
            }
        });
//        if (info != null && mUserType == 0) {
//            convertView.setClickable(true);
//        } else {
//            convertView.setClickable(false);
//        }
        return convertView;
    }

    private void initializeViews(DatiInfo info, ViewHolder viewHolder) {
        viewHolder.titleName.setText(info.getTeacherName());
        viewHolder.scoreLayout.setNumStars(info.getRewardCount());
        String url = info.getQuestionImage();
        if (url != null && url.length() > 0) {
            ViewUtil.Size size = new ViewUtil.Size(ViewUtil.getScreenWidth(mContext), 0);
            String cacheKey = ImageCache.getInstance(mContext).getCacheKey(url, size);
            Bitmap cacheBitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cacheKey);
            if (cacheBitmap != null) {
                viewHolder.datiImage.setImageBitmap(cacheBitmap);
            } else {
                viewHolder.datiImage.setImageResource(R.drawable.zhanweitu);
                ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.datiImage, url, size);
            }
        }

        String teacherlogo_url = info.getTeacherLogo();
        if (teacherlogo_url != null && teacherlogo_url.length() > 0) {
            ViewUtil.Size size = new ViewUtil.Size(ViewUtil.dip2px(mContext, 40), ViewUtil.dip2px(mContext, 40));
            Bitmap cache = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(ImageCache.getInstance(mContext).getCacheKey(teacherlogo_url, size));
            if (cache != null) {
                viewHolder.teacherLogo.setImageBitmap(cache);
            } else {
                viewHolder.teacherLogo.setImageResource(R.drawable.default_head_image);
                ImageCache.getInstance(mContext).loadBitmapToView(viewHolder.teacherLogo, teacherlogo_url, size);
            }
        }

        String grade = null;
        String gradetype = info.getGrade();
        if (gradetype.equals("1")) {
            grade = "一年级";
        } else if (gradetype.equals("2")) {
            grade = "二年级";
        } else if (gradetype.equals("3")) {
            grade = "三年级";
        } else if (gradetype.equals("4")) {
            grade = "四年级";
        } else if (gradetype.equals("5")) {
            grade = "五年级";
        } else if (gradetype.equals("6")) {
            grade = "六年级";
        } else if (gradetype.equals("7")) {
            grade = "初一";
        } else if (gradetype.equals("8")) {
            grade = "初二";
        } else if (gradetype.equals("9")) {
            grade = "初三";
        } else if (gradetype.equals("10")) {
            grade = "高一";
        } else if (gradetype.equals("11")) {
            grade = "高二";
        } else if (gradetype.equals("12")) {
            grade = "高三";
        }

        String subjectNo = null;
        String subjectType = info.getSubjectNo();
        if (subjectType.equals("1")) {
            subjectNo = "数学";
        } else if (subjectType.equals("2")) {
            subjectNo = "外语";
        } else if (subjectType.equals("3")) {
            subjectNo = "综合";
        } else if (subjectType.equals("0")) {
            subjectNo = "语文";
        }

        String questionType = null;

        if (info.getQuestionType().equals("0")) {
            questionType = "填空题";
        } else if (info.getQuestionType().equals("1")) {
            questionType = "选择题";
        }
        viewHolder.datiTag.setText(grade + " " + subjectNo + " " + questionType);
        String answerType = null;
        if (info.getAnswerType().equals("0")) {
            answerType = "必答题";
        } else if (info.getAnswerType().equals("1")) {
            answerType = "抢答题";
        }
        viewHolder.datiType.setText(answerType);
        if (mUserType == 0) {
            viewHolder.mohu.setVisibility(View.GONE);
        } else {
            viewHolder.mohu.setVisibility(View.VISIBLE);
        }
        if (answerType.equals("必答题")){
            viewHolder.mohu.setBackgroundResource(R.drawable.bida);
        }else if (answerType.equals("抢答题")){
            viewHolder.mohu.setBackgroundResource(R.drawable.qiangda);
        }else {

        }
    }

    protected class ViewHolder {
        private RatingBar scoreLayout;
        private TextView titleName;
        private ImageView datiImage;
        private TextView datiType;
        private TextView datiTag;
        private ImageView teacherLogo;
        private ImageView mohu;

        public ViewHolder(View view) {
            scoreLayout = (RatingBar) view.findViewById(R.id.score_layout);
            titleName = (TextView) view.findViewById(R.id.title_name);
            datiImage = (ImageView) view.findViewById(R.id.dati_image);
            datiType = (TextView) view.findViewById(R.id.dati_type);
            datiTag = (TextView) view.findViewById(R.id.dati_tag);
            teacherLogo = (ImageView) view.findViewById(R.id.teacher_logo);
            mohu = (ImageView) view.findViewById(R.id.mohu);
        }
    }
}
